package com.tongji;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.TypedValue;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;


public class AddrDialog extends Activity {
	public static final String SEARCH_QUERY_RESULT_FROM_DIALOG = "SEARCH_DIALOG";
	
	private TextView mTextView;
	private EditText mEditText;
	private Button mButton;
	
	private final Handler mHandler = new Handler(Looper.getMainLooper()) {
		public void handleMessage(Message msg) {
			Bundle data = msg.getData();
			if (data.getString("Commond").equals("notfound")) {
				mTextView.setText(R.string.notice_string);
				mEditText.setVisibility(View.VISIBLE);
				mEditText.setEnabled(true);
				mButton.setEnabled(true);
				Toast.makeText(AddrDialog.this, R.string.notfound_string, Toast.LENGTH_LONG).show();
			}
			else if (data.getString("Commond").equals("found")) {
				int addr = data.getInt("FindingAddr");
				//int port = data.getShort("FindingPort");
				String display = String.format("Server at:%s", ByteTools.int2Ip(addr));
				Toast.makeText(AddrDialog.this, display, Toast.LENGTH_LONG).show();
				returnSearchQuery(ByteTools.int2Ip(addr));
			}
		}
	};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
        // Have the system blur any windows behind this one.
        getWindow().setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
                WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
        
        RelativeLayout container = new RelativeLayout(this);
        
        RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT,
                RelativeLayout.LayoutParams.WRAP_CONTENT);
        lp.addRule(RelativeLayout.CENTER_IN_PARENT);
        
        LinearLayout subContainer = new LinearLayout(this);
        subContainer.setOrientation(LinearLayout.VERTICAL);
        container.addView(subContainer, lp);
        
        mTextView = new TextView(this);
        mTextView.setTextSize(TypedValue.DENSITY_DEFAULT, 20.0f);
        mTextView.setText("Waiting...");
        subContainer.addView(mTextView);
        
        mEditText = new EditText(this);
        mEditText.setVisibility(View.INVISIBLE);
        mEditText.setEnabled(false);
        subContainer.addView(mEditText);
        
        mButton = new Button(this);
        mButton.setText("Ok");
        mButton.setEnabled(false);
        mButton.setOnClickListener(new OnClickListener() {
        	public void onClick(View v) {
        		returnSearchQuery(mEditText.getText().toString());
        	}
        });
        subContainer.addView(mButton);
        
        setContentView(container);
        
        BroadcastThread recv = new BroadcastThread(mHandler);
		recv.start();
	}
	
	private void returnSearchQuery(String result) { 
		Intent resultIntent = new Intent(this, RemoteViewer.class); 
		resultIntent.putExtra(SEARCH_QUERY_RESULT_FROM_DIALOG, result); 
		setResult(Activity.RESULT_OK, resultIntent); 
		finish(); 
	}
}
